﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="weaver.general.*" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.hrm.*" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean" scope="page"/>
<%
    /*
    *@Description: 差旅/交通费用报支-部门
    */
    int formid = Math.abs(Util.getIntValue(request.getParameter("formid")));//表单id
    Map<String, String> mainTableArray = new HashMap<String, String>(); //主表集合

	Map<String, String> detailTableArray3 = new HashMap<String, String>(); //明细3 费用分摊 集合
	Map<String, String> detailTableArray8 = new HashMap<String, String>(); //明细8 费用分摊 集合

    formid = Math.abs(formid);
    String sql = " select b.id,fieldname,detailtable from workflow_billfield b ,workflow_base a where b.billid=-" + formid + " and a.formid=b.billid and (detailtable is null or detailtable = '') ";
    rs.execute(sql);
    while (rs.next()) {
        mainTableArray.put(Util.null2String(rs.getString("fieldname")).toLowerCase(), Util.null2String(rs.getString("id")));
    }

	String sql_detail = "select a.id,a.fieldname from workflow_billfield a,workflow_base b where a.BILLID=b.FORMID and a.billid=-" + formid + " and a.detailtable='formtable_main_" + formid + "_dt3'";
    rs.execute(sql_detail);
    while (rs.next()) {
        detailTableArray3.put(Util.null2String(rs.getString("fieldname")).toLowerCase(), Util.null2String(rs.getString("id")));
    }

    String sql_detail1 = "select a.id,a.fieldname from workflow_billfield a,workflow_base b where a.BILLID=b.FORMID and a.billid=-" + formid + " and a.detailtable='formtable_main_" + formid + "_dt8'";
    rs.execute(sql_detail1);
    while (rs.next()) {
        detailTableArray8.put(Util.null2String(rs.getString("fieldname")).toLowerCase(), Util.null2String(rs.getString("id")));
    }
%>

<script type="text/javascript">
    jQuery(document).ready(function () {
		addEvent(jQuery("#field"+<%=mainTableArray.get("pjhsze")%>));
		addEvent(jQuery("#field"+<%=mainTableArray.get("pjseze")%>));
		jQuery("#\\\\$addbutton2\\\\$").click(function() {
			bindfee3(1);
		})
		bindfee3(2);

		jQuery("#field"+<%=mainTableArray.get("dmbtr")%>).change(function(){
			computeTTJE();
		})
		jQuery("#field"+<%=mainTableArray.get("hth")%>).change(function(){
			computeTTJE();
		})

    })

    function bindfee3(value) {
        var indexnum0 = 0;
        if (document.getElementById("indexnum2")) {
            indexnum0 = document.getElementById("indexnum2").value * 1.0 - 1;
        }

        if (indexnum0 >= 0) {
            if (value == 1) { //当前添加的行
			jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_" + indexnum0).bindPropertyChange(function () {
                    var clxzkm = jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_" + indexnum0).val();
                    if(!getClxzkm(clxzkm)){
                        alert("请勿选择非末级节点");
                        jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0).val("");
                        jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0+"span").text("");
                        jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0+"spanimg").append('<img align="absmiddle" src="/images/BacoError_wev8.gif">');
                    }
                });
                addEvent(jQuery("#field"+<%=detailTableArray3.get("je")%> + "_" + indexnum0));
                if(!indexnum0 == 0){

                    jQuery("#field"+'<%=detailTableArray3.get("fylb1")%>'+"_"+indexnum0).bindPropertyChange(function(){

                        var fylb_yi = jQuery("#field"+'<%=detailTableArray3.get("fylb1")%>'+"_"+0).val();

                        var fylb_now = jQuery("#field"+'<%=detailTableArray3.get("fylb1")%>'+"_"+indexnum0).val();
			
                        if(fylb_now!=fylb_yi){
                            alert("费用类别字段必须一致");
                            jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0).val("");
                            jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0+"span").text('');

                        }
                    });
                }
            } else if (value == 2) {//初始化
                jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_" + indexnum0).bindPropertyChange(function () {
                    var clxzkm = jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_" + indexnum0).val();
                    if(!getClxzkm(clxzkm)){
                        alert("请勿选择非末级节点");
                        jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0).val("");
                        jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0+"span").text("");
                        jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0+"spanimg").append('<img align="absmiddle" src="/images/BacoError_wev8.gif">');
                    }
                });
                if(!indexnum0 == 0){

                    jQuery("#field"+'<%=detailTableArray3.get("fylb1")%>'+"_"+indexnum0).bindPropertyChange(function(){

                        var fylb_yi = jQuery("#field"+'<%=detailTableArray3.get("fylb1")%>'+"_"+0).val();
                        var fylb_now = jQuery("#field"+'<%=detailTableArray3.get("fylb1")%>'+"_"+indexnum0).val();
				
                        if(fylb_now!=fylb_yi){
                            alert("费用类别字段必须一致");
                            jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0).val("");
                            jQuery("#field"+'<%=detailTableArray3.get("clxzkm")%>'+"_"+indexnum0+"span").text('');

                        }
                    });
                }
                }

                jQuery("input[name='check_node_2']").each(function (i) {
                    var indexnum1 = this.value;
                    addEvent(jQuery("#field"+<%=detailTableArray3.get("je")%> + "_" + indexnum1));
                });
            }

    }

	function addEvent($dom){
		$dom.change(function(){
			comput();
		});
	}
	 function getClxzkm(kmbm) {
        var flag = true;
        $.ajax({
            type: "POST",
            dataType: "json",
            async: false,
            url: "/interface/oacs/duss/getClxzkm_Ajax.jsp",
            success: function (data) {
                var kms = data.kms;
                var arr = kms.split(",");
                for (var i = 0; i < arr.length; i++) {
                    if (kmbm == arr[i]) {
                        flag = false;
                    }
                }
            }
        })
        return flag;
    }
</script>

<script type="text/javascript">
/** 计算分摊的税
 *  分摊明细行税额=（分摊含税总额/票据含税总额）* 票据税额总额 * (分摊明细行含税/分摊含税总额)
 *					分摊明细行含税 * 票据税额总额 /票据含税总额
 */
function comput(){
	var pjhsze = jQuery("#field"+<%=mainTableArray.get("pjhsze")%>).val();//票据含税总额
	var pjseze = jQuery("#field"+<%=mainTableArray.get("pjseze")%>).val();//票据税额总额
	var fthsze = jQuery("#field"+<%=mainTableArray.get("sbhj")%>).val();//分摊含税总额
	//alert("ccjtze"+ccjtze);
	jQuery("input[name='check_node_2']").each(function(i){
		var indexnum = this.value

		var ftmxhhs = jQuery("#field"+<%=detailTableArray3.get("je")%> + "_" + indexnum).val();
		console.log("------------------------");
		console.log("票据税额总额 =" + pjseze);
		console.log("票据含税总额） =" + pjhsze);
		console.log("分摊含税总额 =" + fthsze);
		
		console.log("第"+indexnum+"分摊明细行含税 =" + ftmxhhs);
		var ccjtze = jQuery("#sumvalue"+<%=detailTableArray8.get("xjje")%>).val();//出差津贴总额
		//var ftmxhsze = jQuery("#sumvalue"+<%=detailTableArray3.get("je")%>).val();////分摊明细行含税金额
		var aa = accAdd(pjhsze,ccjtze);
		//var ftmxhse = accMul(accMul(aa,pjhsze),ftmxhhs);
		var ftmxhse = accMul(ftmxhhs,pjseze)/aa;
		console.log("ftmxhse=="+ftmxhse);
		console.log("aa=="+aa);
		console.log("ftmxhhs=="+ftmxhhs);
		console.log("pjhsze=="+pjhsze);
		//var ftmxhse = accMul(ftmxhhs , pjseze / pjhsze);
		console.log("分摊明细行税额 =" + ftmxhse);
		
		jQuery("#field"+'<%=detailTableArray3.get("se")%>'+"_"+indexnum).val(ftmxhse);
		jQuery("#field"+'<%=detailTableArray3.get("se")%>'+"_"+indexnum).change();
       // alert($("#sum19738").text());
        var indexim =  document.getElementById("indexnum2").value * 1.0 - 1;
        var ze = $("#field19846").val()*1.00;
        var ze1 = $("#sum19738").text()*1.00;
        if(Math.abs(ze1-ze)>0&&Math.abs(ze1-ze)<1){
            console.log("xx"+$("#field19738_"+indexim).val()*1.00);
            console.log("xx"+ze1);
            console.log("xx"+ze);
            console.log("xx"+(ze-ze1).toFixed(2));
            $("#field19738_"+indexim).val(($("#field19738_"+indexim).val()*1.00+(ze-ze1)).toFixed(2));
            $("#field19738_"+indexim).text(($("#field19738_"+indexim).val()*1.00+(ze-ze1)).toFixed(2));
        }
        // field19738_1
	})
	/**分摊明细行税额=（分摊含税总额/(票据含税总额+出差津贴总额））*票据税额总额*分摊明细行含税金额/分摊含税总额*/
	////** 如果分摊税额总额 != 票据的数额总额 , 把更改分摊的第一行的税额，使其相等 */
	//var ce = accMul(accMul((fthsze/(accAdd(pjhsze,ccjtze))),pjhsze),ftmxhsze)/fthsze;
	var hsze = jQuery("#sumvalue"+<%=detailTableArray3.get("se")%>).val();//分摊明细行税额
	var bb = accAdd(pjhsze,ccjtze)
	var zee =  accMul(fthsze/bb,pjseze);
	var ce = accSub(zee,hsze);
	if(ce != 0){
		var value = jQuery("#field"+'<%=detailTableArray3.get("se")%>'+"_0").val();
		jQuery("#field"+'<%=detailTableArray3.get("se")%>'+"_0").val(accAdd(value,ce));
		jQuery("#field"+'<%=detailTableArray3.get("se")%>'+"_0").change();
		console.log("第一行数额进行差额调整:"+ce);
	}
}

function computeTTJE(){
	var hth = jQuery("#field"+<%=mainTableArray.get("hth")%>).val();
	if(hth == ""){
		jQuery("#field"+<%=mainTableArray.get("ttje")%>).val("");
		return;
	}
	$.ajax({
		type:"POST",
		cache: false,
		url: "/interface/lk/bzd_ajax.jsp?hth="+hth,
		success: function(str){
			var json = eval('(' + str + ')');
			/** 报销总金额 */
			var bxzje = jQuery("#field"+<%=mainTableArray.get("dmbtr")%>).val();
			console.log("报销金额:"+bxzje);
			/** 费用报支单金额  合同号对应的所有报支流程,主表报销金额之和 */
			var fybzdje = json.fybzdje;
			console.log("费用报支单金额:"+fybzdje);
			/** 费用报支未批金额 */
			var fyepje =json.fyepje;
			console.log("费用报支未批金额:"+fyepje);
			/** 已提请未付金额 */
			var ytqwfje = json.ytqwfje;
			console.log("已提请未付金额:"+ytqwfje);
			/** 以付金额 */
			var yfje = json.yfje;
			console.log("以付金额:"+yfje);
			
			/** TTJE=报销总金额+费用报支单金额+费用报支未批金额-已提请未付金额-已付款金额 */
			var ttje = accSub(accAdd( bxzje, accAdd(fybzdje,fyepje)),accAdd(ytqwfje,yfje) );
			console.log("TTJE:"+ttje);
			jQuery("#field"+<%=mainTableArray.get("ttje")%>).val(ttje);
		}
	})
}

</script>

<script type="text/javascript">
    //加法
    function accAdd(arg1,arg2){
        var r1,r2,m;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        return (arg1*m+arg2*m)/m;
    }
    //减法
    function accSub(arg1,arg2){
        var r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        n=(r1>=r2)?r1:r2;
        return ((arg1*m-arg2*m)/m).toFixed(n);
    }
    //乘法
    function accMul(arg1, arg2) {
        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
        try {
            m += s1.split(".")[1].length;
        }
        catch (e) {
        }
        try {
            m += s2.split(".")[1].length;
        }
        catch (e) {
        }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
    }
</script>